package com.example.code.photo;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import org.apache.commons.codec.digest.DigestUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * 一刻相册测试
 */
public class TestYkxc {

    public static void main1(String[] args) throws IOException {

        String album_id = "3506244885419308237";
        String cursor = "";
        String json = "{}";
        String url = "https://photo.baidu.com/youai/album/v1/listfile?clienttype=70&bdstoken=207f35be89e18a126f9cdfcc2d8a8f54&cursor=" + cursor + "&album_id=" + album_id;

        String res = sendPost(url, json);
        System.out.println(res);
    }

    /**
     * 判断url是否是图片
     *
     * @param urlString
     * @return
     */
    public static boolean checkImgExist(String urlString) {
        URL url;
        URLConnection urlConnection;
        try {
            url = new URL(urlString);
            urlConnection = url.openConnection();
            int status = ((HttpURLConnection) urlConnection).getResponseCode();
            if (status == 200) {
                return true;
            }
        } catch (Exception e1) {
            System.out.println("图片不存在!");
        }
        return false;
    }


    public static void exe() throws IOException {
        String filePath = "D:\\DATA\\Pictures\\hetong";
        File[] files = FileUtil.ls(filePath);

        for (int i = 0; i < files.length; i++) {
            File files1 = files[i];
            String md5Hex = DigestUtils.md5Hex(new FileInputStream(files1));
            System.out.println(md5Hex);

            BufferedImage sourceImg = ImageIO.read(new FileInputStream(files1));
            System.out.println(files1.getName());// 源图大小
            System.out.println("大小" + String.format("%.1f", files1.length() / 1024.0));// 源图大小
            System.out.println("宽度" + sourceImg.getWidth()); // 源图宽度
            System.out.println("高度" + sourceImg.getHeight()); // 源图高度


        }

    }


    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            // 设置传递方式
            connection.setRequestMethod("GET");
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.79");
            connection.setRequestProperty("Cookie", "BIDUPSID=1DA23D03D8121BC6E7DC2474E7F004BF; PSTM=1661476176; BDUSS_BFESS=kNvcThZa05LbFF0cGhsUG5XeW1zVi14Q1dOdHRsM3VJMEZReDlGSm43cUFJMnBqRVFBQUFBJCQAAAAAAAAAAAEAAACRcwEyZmx5z~635bLQ1MIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAICWQmOAlkJjN; BAIDUID=4832839B1E82BC32DEE01795B31429D1:SL=0:NR=10:FG=1; MCITY=-268%3A210%3A; newlogin=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; STOKEN=e10565ca9a2a92b0f8fdda4af122240a16a4f69d0bb6768944d60e00403396a0; BAIDUID_BFESS=4832839B1E82BC32DEE01795B31429D1:SL=0:NR=10:FG=1; H_PS_PSSID=36551_38643_38831_39027_39024_38943_38878_38957_38956_39009_39038_38802_39084_26350_39092_39101; BA_HECTOR=0lag00a0a4050184a48ka5ad1ib23c61p; ZFY=mbiA1:Bw219QlbV5iWWf9Z6wh8o2HfwMRpOzSymyTDI0:C; ab_sr=1.0.1_NTg5OWNlNmM3NjNmMTBhYjUxMzBlZmUxZjc4M2U0NjEwOTIyYjNmZjRlNDdjOWVmMGYwMzFmMzM0NDU3MmNlM2ExM2E2YzJmZjFhZTI1M2U5MTM1OWJlZDI2MDQ2NWYwNTkxNmNiYmUzZjAwMDlmMTlmN2EyYWJhOTMxOTdkMDliNzk5YzdmYzNhMjhjOTY2YWY1MjAwMmQ1MWQyYWMzNQ==; csrfToken=z_ZoFT1YPoANh2VWFF5jbIA4; PANWEB=1; PANWEB.sig=mEnYrSeaQqssYZire89rFPmLY9htA0FzmyWp6jBsV1U; sajssdk_2015_cross_new_user=1; Hm_lvt_829488e8924d8de8d4420f2bbed270ca=1688973405,1689147022,1689299253,1689326381; Hm_lpvt_829488e8924d8de8d4420f2bbed270ca=1689326381; PANPSC=4806329934197887703%3AKkwrx6t0uHCb25r6KRvMNXlibiKzYuqjT2bgLWQqRJ1E4GifJDs1w0Dsc%2BX7uesnuXNA240xvNZHItjtGGxI6VVcWqfrbQL63jgXHHAOeF4bLs7fU%2BHz2QoqoRu%2F5gC6YQWlSQC07bYrNXp%2BjVEMQv6PX3R9TkqJdk0GhnyLQJNbyl%2BAzDab%2BkCFPiVTCyYz2fNZfjs7wBY%2FcoIBUQpA2juoAeCl9TBG; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221932976094%22%2C%22first_id%22%3A%2218953b2e6caa08-0b5801d0248c99-7e565470-1382400-18953b2e6cbd46%22%2C%22props%22%3A%7B%7D%2C%22%24device_id%22%3A%2218953b2e6caa08-0b5801d0248c99-7e565470-1382400-18953b2e6cbd46%22%7D");
            connection.setRequestProperty("Host", "photo.baidu.com");

            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        System.out.println("===========");
        result = result.replace("\\/", "/");
        System.out.println(result);
        return result;
    }

    /**
     * @param url   请求接口
     * @param param 需要的json字符串
     * @return :java.lang.String
     * @description : post接口 返回结果字符串
     * @params : [url, param]
     */
    public static String sendPost(String url, String param) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = null;
            // 打开和URL之间的连接
            conn = (HttpURLConnection) realUrl.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");    // POST方法

            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.79");
            conn.setRequestProperty("Cookie", "BIDUPSID=1DA23D03D8121BC6E7DC2474E7F004BF; PSTM=1661476176; BDUSS_BFESS=kNvcThZa05LbFF0cGhsUG5XeW1zVi14Q1dOdHRsM3VJMEZReDlGSm43cUFJMnBqRVFBQUFBJCQAAAAAAAAAAAEAAACRcwEyZmx5z~635bLQ1MIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAICWQmOAlkJjN; BAIDUID=4832839B1E82BC32DEE01795B31429D1:SL=0:NR=10:FG=1; MCITY=-268%3A210%3A; newlogin=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; STOKEN=e10565ca9a2a92b0f8fdda4af122240a16a4f69d0bb6768944d60e00403396a0; BAIDUID_BFESS=4832839B1E82BC32DEE01795B31429D1:SL=0:NR=10:FG=1; H_PS_PSSID=36551_38643_38831_39027_39024_38943_38878_38957_38956_39009_39038_38802_39084_26350_39092_39101; BA_HECTOR=0lag00a0a4050184a48ka5ad1ib23c61p; ZFY=mbiA1:Bw219QlbV5iWWf9Z6wh8o2HfwMRpOzSymyTDI0:C; ab_sr=1.0.1_NTg5OWNlNmM3NjNmMTBhYjUxMzBlZmUxZjc4M2U0NjEwOTIyYjNmZjRlNDdjOWVmMGYwMzFmMzM0NDU3MmNlM2ExM2E2YzJmZjFhZTI1M2U5MTM1OWJlZDI2MDQ2NWYwNTkxNmNiYmUzZjAwMDlmMTlmN2EyYWJhOTMxOTdkMDliNzk5YzdmYzNhMjhjOTY2YWY1MjAwMmQ1MWQyYWMzNQ==; csrfToken=z_ZoFT1YPoANh2VWFF5jbIA4; PANWEB=1; PANWEB.sig=mEnYrSeaQqssYZire89rFPmLY9htA0FzmyWp6jBsV1U; sajssdk_2015_cross_new_user=1; Hm_lvt_829488e8924d8de8d4420f2bbed270ca=1688973405,1689147022,1689299253,1689326381; Hm_lpvt_829488e8924d8de8d4420f2bbed270ca=1689326381; PANPSC=4806329934197887703%3AKkwrx6t0uHCb25r6KRvMNXlibiKzYuqjT2bgLWQqRJ1E4GifJDs1w0Dsc%2BX7uesnuXNA240xvNZHItjtGGxI6VVcWqfrbQL63jgXHHAOeF4bLs7fU%2BHz2QoqoRu%2F5gC6YQWlSQC07bYrNXp%2BjVEMQv6PX3R9TkqJdk0GhnyLQJNbyl%2BAzDab%2BkCFPiVTCyYz2fNZfjs7wBY%2FcoIBUQpA2juoAeCl9TBG; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221932976094%22%2C%22first_id%22%3A%2218953b2e6caa08-0b5801d0248c99-7e565470-1382400-18953b2e6cbd46%22%2C%22props%22%3A%7B%7D%2C%22%24device_id%22%3A%2218953b2e6caa08-0b5801d0248c99-7e565470-1382400-18953b2e6cbd46%22%7D");
            conn.setRequestProperty("Host", "photo.baidu.com");

            conn.connect();
            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
            // 发送请求参数
            out.write(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static void postYkxc() {
        String album_id = "3506244885419308237";
//        String cursor = "eyJsb3YiOjE2ODg0NzYxNjYsImZvdiI6MTY4ODQ3ODM5NCwibGZzaWQiOjQ5MzkyMjI0MzQyOTM1MiwiZmZpc2QiOjIyNDc2OTMwNjUxNjM3NywidiI6MX0";
        String cursor = "";
        String url = "https://photo.baidu.com/youai/album/v1/listfile?clienttype=70&bdstoken=207f35be89e18a126f9cdfcc2d8a8f54&cursor=" + cursor + "&album_id=" + album_id;

        String json = "{}";

        String sendPost = sendPost(url, json);
        System.out.println(sendPost);
    }

    public static void downPic() {
        String url = "https://pcsdata.baidu.com/thumbnail/c5c7e7211p913e80d2cff48ee9dd81fd?fid=1932976094-16051585-403416846262134&rt=pr&sign=FDTAER-yUdy3dSFZ0SVxtzShv1zcMqd-ZTBAj2INOc91wKgAM9JS3LAyxYk%3D&expires=48h&chkv=0&chkbd=0&chkpc=&dp-logid=477756214192318233&dp-callid=0&time=1691460000&bus_no=26&size=c800_u800&quality=100&vuk=-&ft=video";

        HttpUtil.downloadFile(url, "D:\\DATA\\Pictures\\hetong\\t.jpg");
    }
}
